package com.example.content.fragment.search

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.*
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.databinding.FragmentSearchProductLayoutBinding
import com.example.content.ext.convertUrl
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.manager.ActivityMgr
import com.example.content.manager.GlobalMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.bean.UserInfo
import com.example.content.viewmodule.search.SearchViewModel

/**
 * Created by zhudongao
 * Desc
 * Date 5/13/21
 */
class SearchUserFragment : BaseMvvmFragment<FragmentSearchProductLayoutBinding, SearchViewModel>(){

    lateinit var recyclerAdapter: RecyclerAdapter<UserInfo>

    override fun initView() {
        recyclerAdapter = RecyclerAdapter(object : BindListener<UserInfo> {
            override fun convert(holder: BaseViewHolder, item: ItemEntity<UserInfo>) {
                val image = holder.getView<ImageView>(R.id.head_icon)
                context?.let { Glide.with(it).load(item.data.avatar.convertUrl()).into(image) }
                val title = holder.getView<TextView>(R.id.nick_name)
                title.text = item.data.nickname
                val personalDesc = holder.getView<TextView>(R.id.personal_des)
                personalDesc.text = item.data.signature
                val followBtn = holder.getView<TextView>(R.id.follow_btn)
                followBtn.setOnClickListener {
                    val isFollow = (item.data.follow_related != 0)
                    val req = FollowReq(item.data.id, if (isFollow) UN_ACTION else DO_ACTION)
                    viewModel.follow(req)
                }
                when (item.data.follow_related) {
                    FOLLOW_UN -> {
                        followBtnChange(followBtn, R.drawable.ic_follow, R.string.follow_me, R.color.white)
                    }
                    FOLLOW_ALREADY -> {
                        followBtnChange(followBtn, R.drawable.ic_alread_follow, R.string.already_follow, R.color.followTextColor)
                    }
                    FOLLOW_EACH -> {
                        followBtnChange(followBtn, R.drawable.ic_alread_follow, R.string.follow_each_other, R.color.followTextColor)
                    }
                }
            }
        })
        recyclerAdapter.addItemViewByType(MY_FOLLOWERS_ITEM_TYPE, R.layout.item_user_layout)
        dataBinding.searchRecycler.adapter = recyclerAdapter
        dataBinding.searchRecycler.layoutManager = LinearLayoutManager(context)
        recyclerAdapter.setEmptyView(R.layout.empty_pager_layout)
    }

    override fun initListener() {
        GlobalMgr.searchKey.observe(viewLifecycleOwner, {
            if (it != null) {
                viewModel.search(it, SEARCH_PAGE_USER)
            }
        })
        viewModel.userResult.observe(viewLifecycleOwner, {
            if (it.isEmpty()) {
                recyclerAdapter.updateData(emptyList())
            } else {
                recyclerAdapter.data.clear()
                it.forEach {
                    recyclerAdapter.data.add(ItemEntity(MY_FOLLOWERS_ITEM_TYPE, it))
                }
                recyclerAdapter.notifyDataSetChanged()
            }
        })
        recyclerAdapter.setOnItemClickListener { adapter, _, position ->
            val bundle = Bundle()
            bundle.putInt(USER_ID, (adapter.getItem(position) as ItemEntity<UserInfo>).data.id)
            bundle.putInt(KEY_FRAGMENT, PERSONAL_INFO_CENTER)
            ActivityMgr.startFragment(bundle)
        }
    }

    private fun followBtnChange(view:TextView, @DrawableRes resId: Int, @StringRes text: Int, @ColorRes color:Int) {
        view.background =
            context?.let { it1 -> ContextCompat.getDrawable(it1, resId) }
        view.text = context?.getString(text)
        view.setTextColor(ContextCompat.getColor(context!!, color))
    }

    override fun getViewModuleClass(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_search_product_layout
    }

}